<template>
  <div class="login-container">
    <div class="login-wrap">
      <h3 class="login-titie">小U商城后台管理系统</h3>
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        class="demo-ruleForm"
      >
        <el-form-item prop="username">
          <el-input type="text" placeholder="請輸入用戶名" v-model="ruleForm.username" autocomplete="off">
            <template slot="prepend">
              <i class="el-icon-user-solid"></i>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            type="password"
            placeholder="請輸入密碼"
            show-password
            v-model="ruleForm.password"
            autocomplete="off"
          >
            <template slot="prepend">
              <i class="el-icon-lock"></i>
            </template></el-input
          > 
        </el-form-item>
        <el-form-item>
          <el-button
            class="btn-login"
            type="primary"
            @click="submitForm('ruleForm')"
            >提交</el-button
          >
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    var validateUsername = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入用户名"));
      } else {
        // 必须有，否则后面的不执行
        callback();
      }
    };
    var validatePassword = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      }else {
        callback();
      }
    };
    return {
      ruleForm: {
        username: "",
        password: "",

      },
      rules: {
        username: [{ validator: validateUsername, trigger: "blur" }],
        password: [{ validator: validatePassword, trigger: "blur" }],
      },
    };
  },
  methods: {
    submitForm() {
      // validate表单终级效验方法
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
        // valid是系统自动注入的形参，如果他为真代表表单校验通过
          this.$http.post('/api/userlogin',this.ruleForm).then(res=>{
            console.log(res)
            if(res.code==200){
        //       //将登陆凭证写入sessionStore中
              
        //       // 写入登录信息
            sessionStorage.setItem('userinfo',JSON.stringify(res.list))
            // 登录成功的提示
            this.$success('登陆成功')
            // 页面跳转
            this.$router.push('/layout')
            }else{
              this.$error('用户名或密码错误')
            }
          })
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    // 重置
    // resetForm(formName) {
    //   this.$refs[formName].resetFields();
    // },
  },
};
</script>

<style lang="less" scoped>
.login-container {
  height: 100vh;
  background: url("../imags/loginbg.jpg");
  display: flex;
  justify-content: center;
  align-items: center;
  .login-wrap {
    background: #fff;
    width: 400px;
    height: 300px;
    padding: 0 20px;
    .btn-login {
      width: 100%;
    }
  }
  .login-titie {
    color: #409eff;
    text-align: center;
    margin-top: 45px;
    margin-bottom: 20px;
  }
}
</style>